package scu.maqiang.homogenization;

import scu.maqiang.fes.BVPType;
import scu.maqiang.fes.FES2T31;
import scu.maqiang.mesh.Mesh2T3;
import scu.maqiang.mesh.MeshFormat;
import scu.maqiang.numeric.Direct;
import scu.maqiang.numeric.MVO;
import scu.maqiang.numeric.NewIterSSolver;
import scu.maqiang.numeric.SRMatrix;

public class CellSolution2DT31 {

	public static void main(String[] args) {
		String meshFile = "Mesh2D\\RectangleHoleCellMesh.msh";
		Mesh2T3 mesh2D = new Mesh2T3().readMesh(meshFile, MeshFormat.msh);
		FES2T31 fs = new FES2T31(mesh2D);
		int n = fs.GetNdof();
		SRMatrix A0 = new SRMatrix(n);
		double[] RHS0 = new double[n];
		fs.assembleStiff(new double[] {1.0}, BVPType.COMMON, A0);
		fs.assembleSource(new double[] {1.0}, BVPType.COMMON, RHS0);
		
		double[] RHS = RHS0.clone();
		SRMatrix A = new SRMatrix(A0);
		fs.applyBC_MBN(A, RHS, Direct.All, 0.0, 11, 12, 13, 14);
		NewIterSSolver solver = new NewIterSSolver(A);
		double[] x = new double[n];
		solver.PCGSSOR(RHS, x, 1.65, 1);
		mesh2D.toTecplot("SOTSResults\\CellSolutions.dat", x);
		
		double[] xButtom = new double[n];
		RHS = RHS0.clone();
		fs.applyBC_MBN(A, Direct.All, 1, 3, 11, 12, 13, 14);
		fs.applyBC_MBN(RHS, Direct.All, 0.0, 1, 11, 12, 13, 14);
		fs.applyBC_MBN_Vector(RHS, Direct.All, x, 3);
		solver.setMatrix(A);
		solver.PCGSSOR(RHS, xButtom, 1.65, 1);
		mesh2D.toTecplot("SOTSResults\\CellSolutionsButtom.dat", xButtom);

		double[] xTop = new double[n];
		RHS = RHS0.clone();
		A.clear();
		A = A0.copy();
		fs.applyBC_MBN(A, Direct.All, 1, 3, 11, 12, 13, 14);
		fs.applyBC_MBN(RHS, Direct.All, 0.0, 3, 11, 12, 13, 14);
		fs.applyBC_MBN_Vector(RHS, Direct.All, x, 1);
		solver.setMatrix(A);
		solver.PCGSSOR(RHS, xTop, 1.65, 1);
		mesh2D.toTecplot("SOTSResults\\CellSolutionsTop.dat", xTop);

		double[] xLeft = new double[n];
		RHS = RHS0.clone();
		A.clear();
		A = A0.copy();
		fs.applyBC_MBN(A, Direct.All, 2, 4, 11, 12, 13, 14);
		fs.applyBC_MBN(RHS, Direct.All, 0.0, 4, 11, 12, 13, 14);
		fs.applyBC_MBN_Vector(RHS, Direct.All, x, 2);
		solver.setMatrix(A);
		solver.PCGSSOR(RHS, xLeft, 1.65, 1);
		mesh2D.toTecplot("SOTSResults\\CellSolutionsLeft.dat", xLeft);

		double[] xRight = new double[n];
		RHS = RHS0.clone();
		A.clear();
		A = A0.copy();
		fs.applyBC_MBN(A, Direct.All, 2, 4, 11, 12, 13, 14);
		fs.applyBC_MBN(RHS, Direct.All, 0.0, 2, 11, 12, 13, 14);
		fs.applyBC_MBN_Vector(RHS, Direct.All, x, 4);
		solver.setMatrix(A);
		solver.PCGSSOR(RHS, xRight, 1.65, 1);
		mesh2D.toTecplot("SOTSResults\\CellSolutionsnRight.dat", xRight);
		
		double[] xLeftButtom = new double[n];
		RHS = RHS0.clone();
		A.clear();
		A = A0.copy();
		fs.applyBC_MBN(A, Direct.All, 1, 2, 3, 4, 11, 12, 13, 14);
		fs.applyBC_MBN(RHS, Direct.All, 0.0, 1, 4, 11, 12, 13, 14);
		fs.applyBC_MBN_Vector(RHS, Direct.All, xButtom, 2);
		fs.applyBC_MBN_Vector(RHS, Direct.All, xLeft, 3);
		solver.setMatrix(A);
		solver.PCGSSOR(RHS, xLeftButtom, 1.65, 1);
		mesh2D.toTecplot("SOTSResults\\CellSolutionsnLeftButtom.dat", xLeftButtom);
		
		double[] xLeftTop = new double[n];
		RHS = RHS0.clone();
		A.clear();
		A = A0.copy();
		fs.applyBC_MBN(A, Direct.All, 1, 2, 3, 4, 11, 12, 13, 14);
		fs.applyBC_MBN(RHS, Direct.All, 0.0, 3, 4, 11, 12, 13, 14);
		fs.applyBC_MBN_Vector(RHS, Direct.All, xTop, 2);
		fs.applyBC_MBN_Vector(RHS, Direct.All, xLeft, 1);
		solver.setMatrix(A);
		solver.PCGSSOR(RHS, xLeftTop, 1.65, 1);
		mesh2D.toTecplot("SOTSResults\\CellSolutionsnLeftTop.dat", xLeftTop);
		
		double[] xRightButtom = new double[n];
		RHS = RHS0.clone();
		A.clear();
		A = A0.copy();
		fs.applyBC_MBN(A, Direct.All, 1, 2, 3, 4, 11, 12, 13, 14);
		fs.applyBC_MBN(RHS, Direct.All, 0.0, 1, 2, 11, 12, 13, 14);
		fs.applyBC_MBN_Vector(RHS, Direct.All, xButtom, 4);
		fs.applyBC_MBN_Vector(RHS, Direct.All, xRight, 3);
		solver.setMatrix(A);
		solver.PCGSSOR(RHS, xRightButtom, 1.65, 1);
		mesh2D.toTecplot("SOTSResults\\CellSolutionsnRightButtom.dat", xRightButtom);

		double[] xRightTop = new double[n];
		RHS = RHS0.clone();
		A.clear();
		A = A0.copy();
		fs.applyBC_MBN(A, Direct.All, 1, 2, 3, 4, 11, 12, 13, 14);
		fs.applyBC_MBN(RHS, Direct.All, 0.0, 2, 3, 11, 12, 13, 14);
		fs.applyBC_MBN_Vector(RHS, Direct.All, xTop, 4);
		fs.applyBC_MBN_Vector(RHS, Direct.All, xRight, 1);
		solver.setMatrix(A);
		solver.PCGSSOR(RHS, xRightTop, 1.65, 1);
		mesh2D.toTecplot("SOTSResults\\CellSolutionsnRightTop.dat", xRightTop);
		
		MVO.saveArray("SOTSResults\\CellCenter.txt", x);
		MVO.saveArray("SOTSResults\\CellButtom.txt", xButtom);
		MVO.saveArray("SOTSResults\\CellTop.txt", xTop);
		MVO.saveArray("SOTSResults\\CellLeft.txt", xLeft);
		MVO.saveArray("SOTSResults\\CellRight.txt", xRight);
		MVO.saveArray("SOTSResults\\CellLeftButtom.txt", xLeftButtom);
		MVO.saveArray("SOTSResults\\CellLeftTop.txt", xLeftTop);
		MVO.saveArray("SOTSResults\\CellRightButtom.txt", xRightButtom);
		MVO.saveArray("SOTSResults\\CellRightTop.txt", xRightTop);
		
	}
}
